package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.FansListAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.AddFriendTask;
import com.xgkj.diyiketang.task.FansListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.weidget.MyLoadDataListView;

import java.util.ArrayList;
import java.util.List;

public class FansListActivity extends BaseActivity implements MyLoadDataListView.LoadDataListener,FansListAdapter.FansListAdapterListener{

    private Context mContext;
    private MyLoadDataListView mListView;
    private FansListAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private String mUserId;
    private int mFansListTaskId = -1;
    private TextView back ;
    private NoDataUtil noDataUtil;
    private int addFriendTaskId = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fanslist);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        mUserId = intent.getStringExtra("UserId");
        mListView = (MyLoadDataListView) findViewById(R.id.fans_list);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mAdapter = new FansListAdapter(mContext);
        mAdapter.setAdapterListener(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnLoadDataListener(this);
        View emptyView = findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext,emptyView);
        getFansList();
    }

    private void getFansList() {
        if(currentPage == 0){
            mListView.setNoNextPage();
            return;
        }

        if(mFansListTaskId != -1){
            return ;
        }
        FansListTask task = new FansListTask(size,currentPage,mUserId);
        task.addListener(mFansListTaskEventListener);
        mFansListTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    @Override
    public void onLoadMore() {
        getFansList();
    }

    private TaskEventListener mFansListTaskEventListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == mFansListTaskId){
                mFansListTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    mListView.enableLoadMore();
                }
                List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
                if(data != null){
                    fansList = (List<EatShowUserInfo>) data;
                }

                if(fansList != null && fansList.size()>0){
                    mAdapter.updateData(fansList);
                }

                if (mAdapter != null && mAdapter.getCount()>0){
                    noDataUtil.ListViewNoEmpty(mListView);
                }else {
                    noDataUtil.ListViewEmpty(mListView,null,null,-1);
                }

                currentPage = result;
            }else if (taskId==addFriendTaskId){
                ToastUtil.showMessage(mContext,R.string.apply_to_friend_alreay_send);
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    @Override
    public void onClick(String userId) {
        addFriend(userId);
    }

    private void addFriend(String userId){
        AddFriendTask task = new AddFriendTask(userId);
        task.addListener(mFansListTaskEventListener);
        addFriendTaskId = getEatShowApp().getTaskManager().addTask(task);
    }
}
